package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import sun.nio.cs.MS1250;

public class handle implements Runnable {

	private Socket socket = null;
	public handle(Socket socket)
	{
		this.socket=socket;
	}
	private PrintWriter getwriter(Socket socket) throws IOException
	{
		OutputStream sockerout =  socket.getOutputStream();
		return new PrintWriter(sockerout,true);
	}
	private BufferedReader getReader(Socket socket)throws IOException
	{
		InputStream socketin = socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketin));
	}
	public String response(String msg)
	{
		return "The ServerSystem begin to"+msg;
	}
	
	public void run() {
		// TODO Auto-generated method stub
		
		try{
			String msg =null;
			System.out.println("The IP of this connected client is "+socket.getInetAddress().toString()+" The port is "+socket.getPort());
			BufferedReader reader = getReader(socket);
			PrintWriter writer = getwriter(socket);
			while((msg = reader.readLine())!=null){	
			System.out.println("GOT MESSAGE :"+msg+" THE STATUS IS OK!");
			writer.println(response(msg));
			if(msg.equals("exit")) 
			{
				socket.close();
				break;
			}
			}
		}catch(Exception exception)
		{
			System.out.println(exception.getMessage());
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
